/*
 * CadastrarProjeto.java
 *
 * Created on 5 de Julho de 2008, 21:45
 */

package gerenciadorprojetos.forms;

import gerenciadorprojetos.services.ProjetoService;
import gerenciadorprojetos.services.UsuarioService;
import java.awt.Color;
import org.jdesktop.application.Action;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
import java.util.ListIterator;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;

/**
 *
 * @author  eduardo
 */
public class CadastrarProjeto extends javax.swing.JDialog {
    
    Logger logger = Logger.getLogger(CadastrarProjeto.class);

    /** Creates new form CadastrarProjeto */
    public CadastrarProjeto(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        context = new ClassPathXmlApplicationContext("spring-servlet.xml");
        pS = (ProjetoService)context.getBean("projetoService");
        uS = (UsuarioService)context.getBean("usuarioService");
        
        List<String> l = uS.getUsuarios();
        ListIterator<String> it = l.listIterator();
        while (it.hasNext()) 
            jComboBoxExecutor.addItem(it.next());
        
        jComboBoxExecutor.setSelectedItem(uS.getNomeUsuarioLogado());
        
        limparCampos();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextFieldTitulo = new javax.swing.JTextField();
        jLabelTitulo = new javax.swing.JLabel();
        jLabelDescricao = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaDescricao = new javax.swing.JTextArea();
        jButtonIncluir = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabelExecutor = new javax.swing.JLabel();
        jComboBoxExecutor = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gerenciadorprojetos.GerenciadorProjetosApp.class).getContext().getResourceMap(CadastrarProjeto.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jTextFieldTitulo.setText(resourceMap.getString("jTextFieldTitulo.text")); // NOI18N
        jTextFieldTitulo.setName("jTextFieldTitulo"); // NOI18N
        jTextFieldTitulo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldTituloKeyPressed(evt);
            }
        });

        jLabelTitulo.setText(resourceMap.getString("jLabelTitulo.text")); // NOI18N
        jLabelTitulo.setName("jLabelTitulo"); // NOI18N

        jLabelDescricao.setText(resourceMap.getString("jLabelDescricao.text")); // NOI18N
        jLabelDescricao.setName("jLabelDescricao"); // NOI18N

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextAreaDescricao.setColumns(60);
        jTextAreaDescricao.setLineWrap(true);
        jTextAreaDescricao.setRows(5);
        jTextAreaDescricao.setWrapStyleWord(true);
        jTextAreaDescricao.setName("jTextAreaDescricao"); // NOI18N
        jTextAreaDescricao.setVerifyInputWhenFocusTarget(false);
        jTextAreaDescricao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextAreaDescricaoKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTextAreaDescricao);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gerenciadorprojetos.GerenciadorProjetosApp.class).getContext().getActionMap(CadastrarProjeto.class, this);
        jButtonIncluir.setAction(actionMap.get("IncluirProjeto")); // NOI18N
        jButtonIncluir.setName("jButtonIncluir"); // NOI18N

        jButton2.setAction(actionMap.get("Sair")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jLabelExecutor.setText(resourceMap.getString("jLabelExecutor.text")); // NOI18N
        jLabelExecutor.setName("jLabelExecutor"); // NOI18N

        jComboBoxExecutor.setName("jComboBoxExecutor"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabelTitulo)
                            .addComponent(jLabelDescricao)
                            .addComponent(jLabelExecutor))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldTitulo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
                            .addComponent(jComboBoxExecutor, 0, 555, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButtonIncluir, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelTitulo)
                    .addComponent(jTextFieldTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelDescricao))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelExecutor)
                    .addComponent(jComboBoxExecutor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonIncluir)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jTextFieldTituloKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldTituloKeyPressed
    if (jTextFieldTitulo.getText().length() > 40) {
        jTextFieldTitulo.setText(jTextFieldTitulo.getText().substring(0, 40));
    }
}//GEN-LAST:event_jTextFieldTituloKeyPressed

private void jTextAreaDescricaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextAreaDescricaoKeyPressed
    if (jTextAreaDescricao.getText().length() > 255) {
        jTextAreaDescricao.setText(jTextAreaDescricao.getText().substring(0, 255));
    }
}//GEN-LAST:event_jTextAreaDescricaoKeyPressed

    @Action
    public void IncluirProjeto() {
        if (validaCampos()) {
            if (pS.cadastrarProjeto(jTextFieldTitulo.getText(), jTextAreaDescricao.getText(), (String)jComboBoxExecutor.getSelectedItem())) {
                JOptionPane.showMessageDialog(this, "Projeto cadastrado com sucesso !");
                logger.debug("Projeto cadastrado com sucesso !");
                limparCampos();
            } else {
                JOptionPane.showMessageDialog(this, "Ocorreu um erro ao cadastrar o projeto, caso o erro persista favor contacte o analista responsável !");
                logger.debug("Ocorreu um erro ao cadastrar o projeto, caso o erro persista favor contacte o analista responsável !");
            }
        }
    }
    
    public boolean validaCampos() {
        jLabelTitulo.setForeground(Color.BLACK);
        jLabelDescricao.setForeground(Color.BLACK);
        jLabelExecutor.setForeground(Color.BLACK);
        
        boolean ok = true;
        
        if (jTextFieldTitulo.getText() == null || "".equals(jTextFieldTitulo.getText()) ) {
            jLabelTitulo.setForeground(Color.RED);
            ok = false;
        }
        
        if (jTextAreaDescricao.getText() == null || "".equals(jTextAreaDescricao.getText()) ) {
            jLabelDescricao.setForeground(Color.RED);
            ok = false;
        }
        
        if (jComboBoxExecutor.getSelectedIndex() < 0 || jComboBoxExecutor.getSelectedItem().equals("")) {
            jLabelExecutor.setForeground(Color.RED);
            ok = false;
        }
        
        return ok;
    }

    @Action
    public void Sair() {
        setVisible(false);
    }
    
    public void limparCampos()
    {
        jTextFieldTitulo.setText("");
        jTextAreaDescricao.setText("");
        jComboBoxExecutor.setSelectedItem(uS.getUsuarioLogado().getIdUsuario()+" - "+uS.getUsuarioLogado().getNomeUsuario());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButtonIncluir;
    private javax.swing.JComboBox jComboBoxExecutor;
    private javax.swing.JLabel jLabelDescricao;
    private javax.swing.JLabel jLabelExecutor;
    private javax.swing.JLabel jLabelTitulo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaDescricao;
    private javax.swing.JTextField jTextFieldTitulo;
    // End of variables declaration//GEN-END:variables

    private ApplicationContext context;
    private ProjetoService pS;
    private UsuarioService uS;
    
}
